package com.lxl.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/*
* 提供json默认的消息返回格式
* */
public class JsonMsgProvider implements MessageProvider {

    private static DateFormat msgDataFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    @Override
    public String provideMessage(MsgStatus status, String msgContent){
        JSONObject msgObject;

        switch (status){
            case ERROR:
            case REQUEST_ERROR:
            case FILE_ERROR:
            case INTERNAL_SERVER_ERROR:{
                //传入Str串
                msgObject = createMsgEntity(createHeader(status.getIndex()), createJsonObject("object", msgContent));
                break;
            }
            case LONG_TASK_RUN:{
                msgObject = createMsgEntity(createHeader(status.getIndex()), msgContent);
                break;
            }
            case SUCCESS:{
                //传入json格式的字符串
                msgObject = createMsgEntity(createHeader(status.getIndex()), createBody(msgContent));
                break;
            }
            default:{
                msgObject = new JSONObject();
                break;
            }
        }

        return msgObject.toJSONString();
    }

    @Override
    public String provideMessage(MsgStatus status, String msgContent, File templateFile) {
        String msg = null;
        try {
            String templateStr = FileUtils.readFileToString(templateFile);
            TemplateMeta templateMeta = TemplateParser.buildTemplateMetaList(templateStr)[0];//json格式为0 xml格式为1

            if (templateMeta != null){
                TemplateData templateData = TemplateParser.buildTemplateData(status, msgContent, templateMeta);
                TemplateParser.parse(templateData, templateMeta.structure, 0, templateData.arrayLength);
                msg = TemplateParser.buildResult(templateData, templateMeta, "json");
            }else {
                msg = provideMessage(status, msgContent);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return msg;
    }

    //创建消息头部
    protected JSONObject createHeader(int code){
        JSONObject header = new JSONObject();
        header.put("msgCode",code+"");
        header.put("msgId", UUID.randomUUID()+"");
        header.put("createTime", msgDataFormat.format(new Date()));
        return  header;
    }

    //参数格式为json
    protected JSONObject createBody(String jsonContent){
        return JSON.parseObject(jsonContent);
    }

    //创建K:V
    protected JSONObject createJsonObject(String key, String content){
        JSONObject obj = new JSONObject();
        obj.put(key, content);
        return obj;
    }

    protected JSONObject createJsonObject(String key, JSONObject value){
        JSONObject obj = new JSONObject();
        obj.put(key,value);
        return obj;
    }

    //创建整个消息
    protected JSONObject createMsgEntity(JSONObject header, JSONObject body){
        JSONObject msg = new JSONObject();
        msg.put("header", header);
        msg.put("body",body);
        return msg;
    }

    protected JSONObject createMsgEntity(JSONObject header, String content){
        JSONObject msg = new JSONObject();

        msg.put("header", header);
        msg.put("body", content);

        return  msg;
    }
}
